<template>
  <div class="top-category">
    <!-- 面包屑 -->
    <div class="container">
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <transition name="slider" mode="out-in" appear>
          <XtxBreadItem v-if="category.name" :key="Date.now()">{{
            category.name
          }}</XtxBreadItem>
        </transition>
      </XtxBread>
    </div>

    <!-- 轮播图 -->
    <div class="container">
      <XtxCarousel :imgList="imgList" style="height:500px"></XtxCarousel>
    </div>
    <div class="container">
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in category.children" :key="item.id">
            <a href="javascript:;">
              <img :src="item.picture" />
              <p>{{ item.name }}</p>
            </a>
          </li>
        </ul>
      </div>

      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in cateArr" :key="item.id">
        <div class="head">
          <h3>- {{ item.name }} -</h3>
          <p class="tag">{{ item.saleProperties }}</p>
          <XtxMore />
        </div>
        <div class="body">
          <GoodsItem :goods="i" v-for="i in item.goods" :key="i.id" />
        </div>
      </div>
    </div>
    <!-- 不同分类商品 -->
  </div>
</template>

<script>
import GoodsItem from './components/Goods-item.vue'
import { computed, onBeforeUnmount, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { getCategory } from '@/api/category'
export default {
  name: 'category',
  components: { GoodsItem },
  setup () {
    const store = useStore()
    const route = useRoute()
    const category = computed(() => {
      const temp = store.state.category.list.find(
        item => item.id === route.params.id
      )

      return temp || ''
    })

    const imgList = ref([])
    store.dispatch('home/getImgList').then(res => {
      imgList.value = res
    })

    const cateArr = ref([])
    const stop = watch(
      () => route.params.id,
      () => {
        route.name === 'category' &&
          getCategory(route.params.id).then(res => {
            cateArr.value = res.result.children
          })
      },
      {
        immediate: true
      }
    )
    onBeforeUnmount(() => {
      stop()
    })

    return { category, imgList, cateArr }
  }
}
</script>

<style scoped lang="less">
.top-category {
  .XtxCarousel {
    ::v-deeps .carousel-indicator {
      position: absolute;
      left: 0;
      top: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
    }
  }
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }
}
</style>
<style lang="less">
.top-category {
  .XtxCarousel {
    .carousel-indicator {
      position: absolute;
      left: 0;
      top: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
    }
  }
}
</style>
